home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / join < prev    next >
Text File  |  1994-05-12  |  2KB  |  118 lines

  1. //-------------------------------------------------------------------//
  2. //  join
  3.  
  4. //  Syntax: join (s)
  5. //          join (s, delimeter)
  6.  
  7. //  Description:
  8.  
  9. //  Join strings/numbers together to form a single string
  10. //
  11. //  s can be any class (list, matrix, vector) containing 
  12. //  string/numeric data to be concatenated together.
  13. //
  14. //  delimiter is an optional string to be stuffed between 
  15. //  strings/numbers.
  16. // 
  17. //  The output is a single string.
  18. //
  19.  
  20. //  Example 1:
  21. //  > s = ["January","February","March"]
  22. //   s =
  23. //  January   February  March
  24. //  > t = join(s)
  25. //   t =
  26. //  JanuaryFebruaryMarch
  27. //  > t = join(s," ")
  28. //   t =
  29. //  January February March
  30. //  > t = join(s,", ")
  31. //   t =
  32. //  January, February, March
  33. //  
  34.  
  35. //  Example 2:
  36. //  > n = [1,2,3,4,5]
  37. //   n =
  38. //          1          2          3          4          5
  39. //  > t = join(n)
  40. //   t =
  41. //  12345
  42. //  > t = join(n,":")
  43. //   t =
  44. //  1:2:3:4:5
  45. //
  46.  
  47. //  Example 3:
  48. //  > r.[1] = "My zip code is";
  49. //  > r.[2] = 94720;
  50. //  > t = join(r," ")
  51. //   t =
  52. //  My zip code is 94720
  53. //  >
  54.  
  55. //  Example 4:
  56. //  > r = getline("file");
  57. //  > line = join(r," ");
  58. //
  59.  
  60. //  See Also: strsplt, strtod, num2str, getline, sprintf
  61.  
  62. //  Tzong-Shuoh Yang (tsyang@ce.berkeley.edu)  5/10/94
  63. //
  64. //-------------------------------------------------------------------//
  65.  
  66. join = function(s,delimiter)
  67. {  
  68.    local (i,del,n,ts,tmp,txt);
  69.  
  70.    if (exist(delimiter)) {
  71.       if (class(delimiter) != "string") {
  72.          error("Argument delimiter of function join() must be a string");
  73.       }
  74.       del = delimiter;
  75.    else
  76.       del = "";
  77.    }
  78.    
  79.    txt = "";
  80.    
  81.    if (class(s) == "list") {
  82.      n = length(s);
  83.      for (i in 1:n) {
  84.         if (class(s.[i])=="num") {
  85.           if (type(s.[i]) == "real") {
  86.              sprintf(tmp,"%g",s.[i]);
  87.           else
  88.              sprintf(tmp,"%g+%gi",real(s.[i]),imag(s.[i]));
  89.           }
  90.           txt = txt + tmp;
  91.         else
  92.           txt = txt + s.[i];       
  93.         }
  94.         if (i < n) {
  95.            txt = txt + del;
  96.         }
  97.      }
  98.   else
  99.      ts = s[:];
  100.      for (i in 1:ts.n) {
  101.         if (class(ts[i])=="num") {
  102.           if (type(ts[i]) == "real") {
  103.              sprintf(tmp,"%g", ts[i]);
  104.           else
  105.              sprintf(tmp,"%g+%gi",real(ts[i]),imag(ts[i]));
  106.           }        
  107.           txt = txt + tmp;
  108.         else
  109.           txt = txt + ts[i];       
  110.         }
  111.         if (i < ts.n) {
  112.            txt = txt + del;
  113.         }
  114.      }    
  115.   }
  116.   return txt;
  117. };
  118.